package com.whf.Controller;

import com.whf.VO.ResultVO;
import com.whf.converter.OrderForm2OrderDTO;
import com.whf.dto.OrderDTO;
import com.whf.enums.ResultEnum;
import com.whf.exception.SellException;
import com.whf.form.OrderForm;
import com.whf.service.OrderService;
import com.whf.service.WebSocket;
import com.whf.utils.ResultVOUtil;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/15 0015.
 */
@RestController
@RequestMapping("/buyer/order")
@Slf4j
public class BuyerOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private WebSocket webSocket;

    //创建新的订单
    @PostMapping("/create")
    public ResultVO<Map<String,String >> create(@Valid OrderForm orderForm,
                                                BindingResult bindingResult){
        //验证传递的参数是否合法
        if(bindingResult.hasErrors()){
            log.error("[创建order] 传递参数不合法,orderForm={}",orderForm);
            throw new SellException(ResultEnum.ORDER_ERROR_PARAMS.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        //转换参数
        OrderDTO orderDTO = OrderForm2OrderDTO.convert(orderForm);
        //判断购物车中是否有信息
        if(CollectionUtils.isEmpty(orderDTO.getOrderDetailList())){
            log.error("[创建order] 购物车不能为空,orderForm={}",orderForm);
            throw new SellException(ResultEnum.ORDER_ERROR_PARAMS);
        }
        OrderDTO newOrderDTO = orderService.create(orderDTO);
        Map<String,String> data=new HashMap<>();
        data.put("orderId",newOrderDTO.getOrderId());
        //调用websocket,给后台发送提醒信息
        webSocket.sendMessage("您有新的订单了");
        return ResultVOUtil.success(data);



    }

    //查询订单列表
    @GetMapping("/list")
    public ResultVO<List<OrderDTO>> list(@RequestParam("openid") String openid,
                                         @RequestParam(value = "page",defaultValue = "0") Integer page,
                                         @RequestParam(value = "size",defaultValue = "10") Integer size){
        if(StringUtils.isEmpty(openid)){
            log.error("[查询订单列表] 用户openid不能为空");
            throw new SellException(ResultEnum.ORDER_ERROR_PARAMS);
        }
        PageRequest pageRequest=new PageRequest(page,size);
        Page<OrderDTO> orderDTOPage = orderService.findAll(openid, pageRequest);
        List<OrderDTO> orderDTOList = orderDTOPage.getContent();
        return ResultVOUtil.success(orderDTOList);
    }
    //查询订单详情
    @GetMapping("/detail")
    public ResultVO<OrderDTO> detail(@RequestParam("openid") String openid,
                                    @RequestParam("orderId") String orderId){
        if(StringUtils.isEmpty(openid)){
            log.error("[查询订单详情] openid不能为null");
            throw new SellException(ResultEnum.ORDER_ERROR_PARAMS);
        }
        if(StringUtils.isEmpty(orderId)){
            log.error("[查询订单详情] orderId不能为null");
            throw new SellException(ResultEnum.ORDER_ERROR_PARAMS);
        }
        OrderDTO orderDTO = orderService.findOne(orderId);
        if(orderDTO==null){
            log.error("[查询订单详情] 该订单不存在,orderId={}",orderId);
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        if(!orderDTO.getBuyerOpenid().equals(openid)){
            log.error("[查询订单详情] 该订单不属于该用户,orderId={};openid={}",orderId,openid);
            throw new SellException(ResultEnum.ORDER_ERROR_BUYER);
        }
        return ResultVOUtil.success(orderDTO);


    }
    //取消订单
    @PostMapping("/cancel")
    public ResultVO cancel(@RequestParam("openid") String openid,
                           @RequestParam("orderId") String orderId){
        if(StringUtils.isEmpty(openid)){
            log.error("[取消订单] openid不能为null");
            throw new SellException(ResultEnum.ORDER_ERROR_PARAMS);
        }
        if(StringUtils.isEmpty(orderId)){
            log.error("[取消订单] orderId不能为null");
            throw new SellException(ResultEnum.ORDER_ERROR_PARAMS);
        }
        OrderDTO one = orderService.findOne(orderId);
        OrderDTO result = orderService.cancel(one);
        if(result==null){
            log.error("[取消订单] 取消订单失败,orderId={}",orderId);
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        if(!result.getBuyerOpenid().equals(openid)){
            log.error("[取消订单] 该订单不属于该用户,orderId={};openid={}",orderId,openid);
            throw new SellException(ResultEnum.ORDER_ERROR_BUYER);
        }
        return ResultVOUtil.success();


    }

}
